<template>
  <view class="diagnosis-page">
    <!-- 状态栏占位 -->
    <view class="status-bar"></view>
    
    <!-- 头部 -->
    <view class="header">
      <view class="header-content">
        <view class="app-info">
          <view class="app-icon">
            <view class="icon-circle"></view>
          </view>
          <view class="app-text">
            <text class="app-title">诊断记录</text>
            <text class="app-subtitle">DIAGNOSIS RECORDS</text>
          </view>
        </view>
        <view class="user-avatar">
          <text class="avatar-icon">👤</text>
        </view>
      </view>
    </view>

    <!-- 记录统计 -->
    <view class="stats-section">
      <view class="stats-card">
        <text class="stats-title">总诊断次数</text>
        <text class="stats-number">{{ records.length }}</text>
      </view>
    </view>

    <!-- 记录列表 -->
    <view class="records-section">
      <view class="section-title">
        <view class="title-line"></view>
        <text class="title-text">诊断历史</text>
      </view>
      
      <scroll-view 
        class="records-scroll" 
        scroll-y="true"
        :refresher-enabled="true"
        :refresher-triggered="refreshing"
        @refresherrefresh="onRefresh"
      >
        <view class="records-list">
          <view 
            v-for="record in records" 
            :key="record.id" 
            class="record-item"
            @tap="showRecordDetail(record)"
          >
            <view class="record-header">
              <view class="record-time">
                {{ formatTime(record.diagnosisTime) }}
              </view>
              <view class="record-type" :class="getTypeClass(record.diagnosisType)">
                {{ record.diagnosisType }}
              </view>
            </view>
            
            <view class="record-content">
              <view class="record-result" :class="getResultClass(record.diagnosisResult)">
                <text class="result-text">{{ record.diagnosisResult }}</text>
                <view class="result-indicator" :class="getResultClass(record.diagnosisResult)"></view>
              </view>
              
              <view class="record-details">
                <view class="detail-row">
                  <text class="detail-label">信号强度:</text>
                  <text class="detail-value" :class="getSignalClass(record.signalStrength)">
                    {{ record.signalStrength }}
                  </text>
                </view>
                <view class="detail-row">
                  <text class="detail-label">网络类型:</text>
                  <text class="detail-value">{{ record.networkType }}</text>
                </view>
                <view class="detail-row">
                  <text class="detail-label">下载速度:</text>
                  <text class="detail-value">{{ record.downloadSpeed }}</text>
                </view>
              </view>
              
              <view class="record-location">
                <text class="location-icon">📍</text>
                <text class="location-text">{{ record.location }} · {{ record.deviceModel }}</text>
              </view>
            </view>
          </view>
        </view>
        
        <view v-if="loading" class="loading">
          <!-- <uni-load-more status="loading"></uni-load-more> -->
        </view>
        
        <view v-if="records.length === 0 && !loading" class="empty-state">
          <text class="empty-icon">📊</text>
          <text class="empty-text">暂无诊断记录</text>
        </view>
      </scroll-view>
    </view>



  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import historyApi from '../../api/historyApi'
// 响应式数据
const records = ref([])
const loading = ref(false)
const refreshing = ref(false)
const selectedRecord = ref(null)
const detailPopup = ref(null)

// 获取诊断记录
const fetchRecords = async () => {
    historyApi.getHistoryById('5').then(response =>{
		console.log(response)
		records.value = response.data;
		
	}).catch(err => {
		console.log(err);
	})
   
  } 

// 下拉刷新
const onRefresh = () => {
  refreshing.value = true
  fetchRecords()
}

// 格式化时间
const formatTime = (timeString) => {
  const date = new Date(timeString)
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hours = String(date.getHours()).padStart(2, '0')
  const minutes = String(date.getMinutes()).padStart(2, '0')
  return `${month}-${day} ${hours}:${minutes}`
}

// 获取诊断类型样式
const getTypeClass = (type) => {
  const typeMap = {
    '常规诊断': 'type-normal',
    '深度诊断': 'type-deep',
    '故障诊断': 'type-fault',
    '性能诊断': 'type-performance',
    '稳定性诊断': 'type-stability'
  }
  return typeMap[type] || 'type-normal'
}

// 获取结果样式
const getResultClass = (result) => {
  if (result.includes('正常') || result.includes('良好') || result.includes('稳定')) {
    return 'result-good'
  } else if (result.includes('中断') || result.includes('无信号') || result.includes('故障')) {
    return 'result-bad'
  } else {
    return 'result-warning'
  }
}

// 获取信号强度样式
const getSignalClass = (strength) => {
  const strengthMap = {
    '良好': 'signal-good',
    '中等': 'signal-medium',
    '差': 'signal-bad',
    '无信号': 'signal-none'
  }
  return strengthMap[strength] || 'signal-medium'
}

// 显示记录详情
const showRecordDetail = (record) => {
  selectedRecord.value = record
  detailPopup.value.open()
}

// 关闭详情
const closeDetail = () => {
  detailPopup.value.close()
}

// 导航
const navigateTo = (page) => {
  uni.showToast({
    title: `跳转到${page}`,
    icon: 'none'
  })
}

// 页面加载
onMounted(() => {
  fetchRecords()
})
</script>

<style scoped>
.diagnosis-page {
  min-height: 100vh;
  background: linear-gradient(180deg, #000000 0%, #1a0a2e 50%, #16213e 100%);
  color: #ffffff;
}

.status-bar {
  height: 44px;
  background: transparent;
}

.header {
  padding: 0 20px 20px;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.app-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.app-icon {
  width: 40px;
  height: 40px;
  position: relative;
}

.icon-circle {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
}

.icon-circle::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #00ffff;
}

.app-text {
  display: flex;
  flex-direction: column;
}

.app-title {
  font-size: 18px;
  font-weight: bold;
  color: #ffffff;
}

.app-subtitle {
  font-size: 12px;
  color: #888888;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}

.avatar-icon {
  font-size: 20px;
}

.stats-section {
  padding: 0 20px 20px;
}

.stats-card {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 16px;
  padding: 20px;
  text-align: center;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.stats-title {
  font-size: 14px;
  color: #cccccc;
  display: block;
  margin-bottom: 8px;
}

.stats-number {
  font-size: 32px;
  font-weight: bold;
  color: #00ffff;
  display: block;
}

.records-section {
  flex: 1;
  padding: 0 20px;
}

.section-title {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  gap: 12px;
}

.title-line {
  width: 4px;
  height: 20px;
  background: linear-gradient(180deg, #ff6b6b 0%, #4ecdc4 100%);
  border-radius: 2px;
}

.title-text {
  font-size: 18px;
  font-weight: bold;
  color: #ffffff;
}

.records-scroll {
  height: 530px;
}

.records-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.record-item {
  background: rgba(255, 255, 255, 0.08);
  border-radius: 16px;
  padding: 16px;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.record-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.record-time {
  font-size: 14px;
  color: #cccccc;
}

.record-type {
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: bold;
}

.type-normal {
  background: rgba(76, 175, 80, 0.2);
  color: #4caf50;
}

.type-deep {
  background: rgba(33, 150, 243, 0.2);
  color: #2196f3;
}

.type-fault {
  background: rgba(244, 67, 54, 0.2);
  color: #f44336;
}

.type-performance {
  background: rgba(255, 152, 0, 0.2);
  color: #ff9800;
}

.type-stability {
  background: rgba(156, 39, 176, 0.2);
  color: #9c27b0;
}

.record-content {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.record-result {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.result-text {
  font-size: 16px;
  font-weight: bold;
}

.result-indicator {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.result-good {
  color: #4caf50;
}

.result-good .result-indicator {
  background: #4caf50;
}

.result-warning {
  color: #ff9800;
}

.result-warning .result-indicator {
  background: #ff9800;
}

.result-bad {
  color: #f44336;
}

.result-bad .result-indicator {
  background: #f44336;
}

.record-details {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.detail-row {
  display: flex;
  justify-content: space-between;
  font-size: 14px;
}

.detail-label {
  color: #cccccc;
}

.detail-value {
  color: #ffffff;
}

.signal-good {
  color: #4caf50;
}

.signal-medium {
  color: #ff9800;
}

.signal-bad {
  color: #f44336;
}

.signal-none {
  color: #666666;
}

.record-location {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: #888888;
}

.location-icon {
  font-size: 12px;
}

.loading {
  padding: 20px;
  text-align: center;
}

.empty-state {
  padding: 60px 20px;
  text-align: center;
}

.empty-icon {
  font-size: 48px;
  display: block;
  margin-bottom: 16px;
}

.empty-text {
  font-size: 16px;
  color: #888888;
}

.bottom-nav {
  display: flex;
  background: rgba(0, 0, 0, 0.8);
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  padding: 8px 0;
}

.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 8px;
}

.nav-item.active {
  color: #00ffff;
}

.nav-icon {
  font-size: 20px;
}

.nav-text {
  font-size: 12px;
}

.detail-popup {
  background: #1a1a1a;
  border-radius: 20px 20px 0 0;
  max-height: 80vh;
}

.popup-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.popup-title {
  font-size: 18px;
  font-weight: bold;
  color: #ffffff;
}

.popup-close {
  font-size: 20px;
  color: #888888;
  padding: 4px;
}

.popup-content {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.detail-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.detail-title {
  font-size: 16px;
  font-weight: bold;
  color: #00ffff;
}

.detail-desc {
  font-size: 14px;
  color: #cccccc;
  line-height: 1.5;
}

.network-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.info-item {
  font-size: 14px;
  color: #cccccc;
}
</style>